//
//  SYTuDiShiXunViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/24.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYTuDiShiXunViewController.h"
#import "SYShiXunHeaderView.h"
#import "SYWanChengTableViewCell.h"
#import "SYChengYuanDetailsView.h"
#import "SYAttendLectureTableViewCell.h"
#import "SYAttendZhuZiTableViewCell.h"
#import "SYGuoKeTableViewCell.h"
#import "SYResourcesTableViewCell.h"
#import "SYXinDeView.h"
#import "SYTuDiShiXunJiLuView.h"
#import "SYTuDiTingKeXinXiView.h"


@interface SYTuDiShiXunViewController () <UITableViewDelegate,UITableViewDataSource,SYWanChengTableViewCellDelegate,SYAttendLectureTableDelegate,SYResourcesTableViewDelegate>
@property (strong, nonatomic) SYTopView *topView;
@property (nonatomic, strong) UIButton *addBtn;//添加按钮
@property (nonatomic, strong) UITableView *curTableView;//表
@property (nonatomic, strong) SYChengYuanDetailsView *cyDetailsView;//成员
@property (strong, nonatomic) SYXinDeView *xindeView;//学习心得
@property (strong, nonatomic) SYTuDiShiXunJiLuView *tdVC;//师训记录
@property (strong, nonatomic) SYTuDiTingKeXinXiView *tdView;//听课信息


@end

@implementation SYTuDiShiXunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    [self initSubView];
    
}

- (void)initSubView {
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45);
    }];
    
    [self.view addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(25);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(40);
    }];
    //表
    [self.view addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(0);
        make.left.equalTo(self.view).offset(14);
        make.right.equalTo(self.view.mas_right).offset(-14);
        make.bottom.equalTo(self.view.mas_bottom).offset(-14);
    }];
}
#pragma mark - tab
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 223+40+40;
    } else {
        return 40;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {//小组成员
        SYWanChengTableViewCell *wCell =[tableView dequeueReusableCellWithIdentifier:@"SYWanChengTableViewCell" forIndexPath:indexPath];
        UIColor *color = nil;
        if (indexPath.row%2) {
            color = [UIColor whiteColor];
            
        } else {
            color = [UIColor colorWithHexString:@"F8F8F8"];
        }
        wCell.selectionStyle = UITableViewCellSelectionStyleNone;
        wCell.delegate = self;
        [wCell incomingDataInteger:1 backColor:color];
        return wCell;
    } else if (indexPath.section == 1 || indexPath.section == 3) {//听课信息 空降信息
        SYAttendLectureTableViewCell *attendCell = [tableView dequeueReusableCellWithIdentifier:@"SYAttendLectureTableViewCell" forIndexPath:indexPath];
        attendCell.selectionStyle = UITableViewCellSelectionStyleNone;
        attendCell.delegate = self;
        if (indexPath.section == 3) {//空讲
            [attendCell attendLectureTableViewCellIndexPath:indexPath stusa:2];
        } else {
            [attendCell attendLectureTableViewCellIndexPath:indexPath stusa:0];
        }
        return attendCell;
    } else if (indexPath.section == 2) {//逐字稿
        SYAttendZhuZiTableViewCell *zzCell = [tableView dequeueReusableCellWithIdentifier:@"SYAttendZhuZiTableViewCell" forIndexPath:indexPath];
        zzCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [zzCell attendZhuZiTableViewCellIndexPath:indexPath];
        return zzCell;
    } else if (indexPath.section == 4) { //过客信息
        SYGuoKeTableViewCell *gkCell = [tableView dequeueReusableCellWithIdentifier:@"SYGuoKeTableViewCell" forIndexPath:indexPath];
        gkCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [gkCell attendGuoKeTableViewCellIndexPath:indexPath];
        return gkCell;
    } else { //资源申请信息
        SYResourcesTableViewCell *gkCell = [tableView dequeueReusableCellWithIdentifier:@"SYResourcesTableViewCell" forIndexPath:indexPath];
        gkCell.selectionStyle = UITableViewCellSelectionStyleNone;
        gkCell.delegate = self;
        [gkCell lncomingData:[SYZiYuanShenQingModel monishuju] indexPath:indexPath];
        return gkCell;
    }
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    if (section == 0) {
        SYShiXunHeaderView *headerView = [[SYShiXunHeaderView alloc] init];
        headerView.backgroundColor = [UIColor whiteColor];
        
        [view addSubview:headerView];
        headerView.biaoView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(view).offset(14);
            make.right.equalTo(view).offset(-14);
            make.bottom.equalTo(view.mas_bottom).offset(0);
        }];
        
        [headerView.topView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(headerView).offset(0);
        }];
        headerView.biaoBgView.hidden = NO;
//
//        [headerView.biaoView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(headerView.mas_left).offset(0);
//            make.right.equalTo(headerView.mas_right).offset(0);
//        }];
        [headerView shixunHeaderViewType:SYShiXunHeaderViewTypeDefault index:1];
//        headerView.index = 1;
    } else {//小组成员
        SYTopView *topView = [[SYTopView alloc] init];
        topView.fgxView.hidden = YES;
        [view addSubview:topView];
        topView.backgroundColor = [UIColor colorWithHexString:@"F8F8F8"];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(view);
            make.left.equalTo(view.mas_left).offset(14);
            make.right.equalTo(view.mas_right).offset(-14);
        }];
        UIButton *guanliBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        guanliBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [guanliBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:0];
        [guanliBtn addTarget:self action:@selector(guanliBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [guanliBtn setTitle:@"管理" forState:UIControlStateNormal];
        guanliBtn.tag = section;
        [view addSubview:guanliBtn];
        [guanliBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(topView.mas_centerY);
            make.right.equalTo(view.mas_right).offset(-50);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(40);
        }];
        
        if (section == 1){//听课信息
            topView.titleLab.text = @"听课信息";
        } else if (section == 2) {//柱子高
            topView.titleLab.text = @"逐字稿";
        } else if (section == 3) {//空讲信息
            topView.titleLab.text = @"空讲信息";
        } else if (section == 4) {//过客信息
            topView.titleLab.text = @"过课信息";
        } else if (section == 5) {//资源申请
            [guanliBtn setTitle:@"查看" forState:UIControlStateNormal];
            topView.titleLab.text = @"资源申请信息";
        }
    }
    UIView *fgxView = [UIView new];
    fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    [view addSubview:fgxView];
    [fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view.mas_bottom).offset(0);
        make.left.equalTo(view.mas_left).offset(28);
        make.right.equalTo(view.mas_right).offset(-28);
        make.height.mas_equalTo(1);
    }];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    UIView *topView = [UIView new];
    topView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    [view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.left.equalTo(view.mas_left).offset(14);
        make.right.equalTo(view.mas_right).offset(-14);
        make.height.mas_equalTo(20);
    }];
    UIView *fgxView = [UIView new];
    fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    [view addSubview:fgxView];
    [fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(0);
        make.left.equalTo(view.mas_left).offset(28);
        make.right.equalTo(view.mas_right).offset(-28);
        make.height.mas_equalTo(1);
    }];
    return view;
}

#pragma mark - SYAttendLectureTableDelegate
//听课信息
- (void)attendLectureTableType:(SYDetailsType)type {
    _xindeView = [[SYXinDeView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20) detailsType:type xindeViewType:SYXinDeViewTypeTuDi];
    [self.view addSubview:_xindeView];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.xindeView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
    }];
}
#pragma mark - cell得代理
//代理
- (void)selectTableViewCellType:(SYWanChengTableViewCellType)type {
    if (type == SYWanChengTableViewCellTypeChaKan) {//查看
        _cyDetailsView = [[SYChengYuanDetailsView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
        [self.view addSubview:_cyDetailsView];
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:kAnimate animations:^{
            weakSelf.cyDetailsView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
        }];
    } else {//移出
        
    }
}
- (void)resourcesTableView:(SYDetailsType)type {
    [self attendLectureTableType:type];
}
//师训记录
- (void)addBtnClick {
    _tdVC = [[SYTuDiShiXunJiLuView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
    [self.view addSubview:_tdVC];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.tdVC.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
    }];
    
}
//管理btn
- (void)guanliBtnClick:(UIButton *)btn {
    NSInteger tage = btn.tag;//1 听课信息 2逐字稿 3空讲信息 4资源申请
    if (tage == 1) {
       
        _tdView = [[SYTuDiTingKeXinXiView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
        [self.view addSubview:_tdView];
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:kAnimate animations:^{
            weakSelf.tdView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
        }];
    }
}

- (SYTopView *)topView {
    if (!_topView) {
        _topView = [[SYTopView alloc] init];
        _topView.titleLab.text = @"师训";
    }
    return _topView;
}
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
       // [_addBtn setImage:[UIImage imageNamed:@"add_teach"] forState:UIControlStateNormal];
        //        _addBtn.hidden = YES;
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_addBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:0];
        [_addBtn setTitle:@"师训记录" forState:UIControlStateNormal];
    }
    return _addBtn;
}
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
//        [_curTableView registerCellWithClassNames:@[@"SYWanChengTableViewCell"]];
        [_curTableView registerCellWithClassNames:@[@"SYWanChengTableViewCell",@"SYResourcesTableViewCell"]];
        [_curTableView registerCellWithNIbNames:@[@"SYAttendLectureTableViewCell",@"SYAttendZhuZiTableViewCell",@"SYGuoKeTableViewCell"]];
    }
    return _curTableView;
}
@end
